package com.ideal.manage.esms.controller.project;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.UserManage;
import com.ideal.manage.esms.bean.project.UserManageDTO;
import com.ideal.manage.esms.bean.project.VehicleResouceDTO;
import com.ideal.manage.esms.bean.project.brand.TreeAlphaBO;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.ParamSettingManageService;
import com.ideal.manage.esms.service.project.ProjectService;
import com.ideal.manage.esms.service.project.ShopCarManageService;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/shopCarManage")
public class ShopCartManageController {
    @Autowired
    ShopCarManageService shopCarManageService;
    @Autowired
    ParamSettingManageService paramSettingManageService;


    @RequestMapping("shopCarList")
    public void userList(Model model) {
        System.out.println("shopCarList");
        Map<String, TreeAlphaBO> displayBrandMaps = paramSettingManageService.getDisplayBrandList();
        model.addAttribute("brandMaps", displayBrandMaps);
}

    @RequestMapping("shopCarData")
    @ResponseBody
    public PageDto userData(int pageNum, HttpServletRequest request) {

        Page<VehicleResouceDTO> page = shopCarManageService.findAllParameterInfos(pageNum,request);
        List<VehicleResouceDTO> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("shopCarAdd")
    @ResponseBody
    public Result shopCarAdd(Model model, VehicleResouceDTO vehicleResouceDTO) throws IOException {
        Result result = new Result();
        UserManage manage = new UserManage();
        System.out.println(1111);
        shopCarManageService.addShopCar(vehicleResouceDTO);
        String brand = vehicleResouceDTO.getBrand();
        System.out.println("brand"+brand);
        String baseUrl = "https://mcc.mtmche.cn/datalake/v1/userManages/ordercode/"+brand;
        URL realUrl = new URL(baseUrl);
        CloseableHttpClient httpCilent2 = HttpClients.createDefault();
        HttpGet httpGet2 = new HttpGet(baseUrl);
        HttpResponse httpResponse = httpCilent2.execute(httpGet2);
        if(httpResponse.getStatusLine().getStatusCode() == 200){
           String srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
            System.out.println(srtResult);
        }
        result.setType("alert");
        result.setMessage("操作成功!");
        return result;
    }

    @RequestMapping("vehicle_del")
    @ResponseBody
    public Result userDelete(Model model, String id) {
        System.out.println(1111);
        Result result = new Result();
        shopCarManageService.deleteById(id);
        result.setType("alert");
        result.setMessage("操作成功!");
        return result;
    }


    @RequestMapping("deploy_edit_data")
    @ResponseBody
    public JSONArray deployEditData(Long id,Model model) {
        JSONArray list = new JSONArray();
        JSONObject jsonObject;
        for (int i = 1; i < 5; i++) {
            jsonObject = new JSONObject();
            // 假装一下null
            jsonObject.put("id", i);
            jsonObject.put("name", "比亚迪");
            jsonObject.put("pid", "type");
            list.add(jsonObject);
        }
        return list;
    }
}
